from view.endSrc.Dataset import Dataset
from view.endSrc.GaussianClusters import GaussianClusters
from view.endSrc.tDataset import tDataset

class MixedGaussianDataset(Dataset):

    # Dataset.__new__(cls, datasetMat, labels, trueCentres=None, className='Dataset'):
    def __new__(cls, datasetMat, labels, gc: GaussianClusters):
        '''
                Need not check gaussianClusters, since we already check it in GaussianClusters.__init__
        :param datasetMat:
        :param labels:
        :param gaussianClusters:
        :return:
        '''

        self = Dataset.__new__(cls, datasetMat, labels, gc.meanList, className='MixedGaussianDataset')
        return self


    def __init__(self, datasetMat, labels, gc: GaussianClusters):

        Dataset.__init__(self, datasetMat, labels, gc.meanList, className='MixedGaussianDataset')

        self.gc = gc
        self.t.mixedParams = self.gc.dumpParamsToJSONDict()


    @staticmethod
    def fromDB(sqlConn, dbId):

        '''
            The factory is used to create objects of Dataset from DB
        :param sqlConn:
        :param dbId:
        :return:
        '''

        t = tDataset(sqlConn)
        if not t.readDataset(dbId):
            sqlConn.logger.write('MixedGaussianDataset.fromDB..failed to read dataset from dbId= ' + str(dbId))
            return None

        gc = GaussianClusters.fromJSONDict(t.mixedParams, t.m_labels)

        mg = MixedGaussianDataset(t.m_dataset, t.m_labels, gc)
        mg.t = t    # set it back, since here all members of t are validated, gotten from DB
        return mg


    def saveToDB(self, sqlConn, datasetName):

        self.t.name = datasetName
        return Dataset.saveToDB(self, sqlConn, self.t.mixedParams)


    